﻿using Cart.BLL;
using Cart.DAL;
using Cart.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cart.Bll
{
    public class GoodsBll : BaseBll<Goods>
    {
        public override void SetDal()
        {
            Dal = new BaseDal<Goods>();
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="name">名称Or内容</param>
        /// <param name="page"> 页码</param>
        /// <param name="size">页码数量</param>
        /// <param name="count">总条目</param>
        /// <returns></returns>
        public List<Goods> GetGoodsByPage(string name, int page, int size, out int count)
        {
            var users = base.Query();
            var list = users;
            if (!string.IsNullOrWhiteSpace(name))
            {
                list = users.Where(u => u.name.Contains(name) || u.content.Contains(name));
            }

            count = list.Count();

            list = list.OrderBy(u => u.gId).Skip((page - 1) * size).Take(size);

            return list.ToList();
        }
        /// <summary>
        /// 商品修改
        /// </summary>
        /// <param name="good">商品</param>
        /// <returns></returns>
        public bool UpdateGood(Goods good)
        {
            var data = Dal.Query().Where(a => a.gId == good.gId).FirstOrDefault();
            //1. 修改商品信息信息表
            data.name = good.name;
            data.price = good.price;
            data.unit = good.unit;
            data.gtId = good.gtId;
            data.use_property = good.use_property;
            data.use_spec = false;
            data.content = good.content;
            data.images = good.images;
            data.sort = good.sort;
            var kdal = new GoodsPropertyKeyDal();
            var vdal = new GoodsPropertyValueDal();

            //删除属性值的数据
            foreach (var item in data.GoodsPropertyKey)
            {
                vdal.Delete(item.GoodsPropertyValue.ToList());
            }
            //删除属性数据
            kdal.Delete(data.GoodsPropertyKey.ToList());

            // 添加键值数据 里面包括了value 值 第六张表的值
            kdal.Add(good.GoodsPropertyKey.ToList());

            var gdal = new GoodsDAL();
            gdal.Update(data);
            return Dal.SaveChanges();
        }
    
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">商品id集合</param>
        /// <returns></returns>
        /// 

        public bool DeleteBatchGood(int[] ids)
        {
            //商品集合
          var data=  Dal.Query().Where(a => ids.Contains(a.gId));
            //先删除 最外面的表
            var kdal = new GoodsPropertyKeyDal();
            var vdal = new GoodsPropertyValueDal();
            foreach (var item in data)
            {
                foreach (var aa in item.GoodsPropertyKey)
                {
                    vdal.Delete(aa.GoodsPropertyValue.ToList());
                }
            }

            //再删键表
            foreach (var item in data)
            {
                kdal.Delete(item.GoodsPropertyKey.ToList());
            }
            //在删除商品表
            var gdal = new GoodsDAL();
            gdal.Delete(data.ToList());
            return Dal.SaveChanges();
        } 
        /// </summary>
        /// <param name="id">商品编号</param>
        /// <returns></returns>
        ///

        public bool DeleteGood(int id) 
        {
            var data = Dal.Query().Where(a=>a.gId==id).FirstOrDefault();

            var kdal = new GoodsPropertyKeyDal();
            var vdal= new GoodsPropertyValueDal();
            //删除属性值表
            foreach (var item in data.GoodsPropertyKey)
            {
                vdal.Delete(item.GoodsPropertyValue.ToList());
            }

            //删除属性表
            kdal.Delete(data.GoodsPropertyKey.ToList());

            //删除商品

            var gdal = new GoodsDAL();
            gdal.Delete(data);

            return Dal.SaveChanges();
        }


    }
}
